/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bo;

import org.ipti.br.tag.dao.SynchControllerFacade;
import org.ipti.br.tag.entity.AbstractEntity;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.AdditionalInfo;
import org.ipti.br.tag.entity.Address;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Configurations;
import org.ipti.br.tag.entity.Deletes;
import org.ipti.br.tag.entity.Hb;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Juridic;
import org.ipti.br.tag.entity.Login;
import org.ipti.br.tag.entity.Neighborhood;
import org.ipti.br.tag.entity.Performance;
import org.ipti.br.tag.entity.PerformanceFrequency;
import org.ipti.br.tag.entity.Plan;
import org.ipti.br.tag.entity.QuestResult;
import org.ipti.br.tag.entity.QuestionsPerson;
import org.ipti.br.tag.entity.SimpleFrequency;
import org.ipti.br.tag.entity.Street;
import org.ipti.br.tag.entity.SynchController;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.UnitySchedule;
import org.ipti.br.tag.entity.WeightAndHeight;
import org.ipti.br.tag.entity.Work;
import org.ipti.br.tag.entity.WorkTopic;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.LoggerUtil;
import org.ipti.br.tag.util.ZipUtils;

/**
 *
 * @author luancorumba
 */
@Stateless
public class SyncBO implements Serializable{
    
    private final String RETURNSPATH = "sync/received/";
    private final String GOPATH = "sync/send/";
    
    @Inject
    private SynchControllerFacade synchControllerDAO;
    @Inject
    private NeighborhoodBO NeighborhoodBO;
    @Inject
    private StreetBO StreetBO;
    @Inject
    private AddressBO addressBO;
    @Inject
    private HumanBO humanBO;
    @Inject
    private JuridicBO juridicBO;
    @Inject
    private AdditionalInfoBO additionalInfoBO;
    @Inject
    private QuestionsPersonBO questionsPersonBO;
    @Inject
    private QuestResultBO questResultBO;
    @Inject
    private LoginBO loginBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private PlanBO planBO;
    @Inject
    private ActorBO actorBO;
    @Inject
    private PerformanceBO performanceBO;
    @Inject
    private PerformanceFrequencyBO performanceFrequencyBO;
    @Inject
    private SimpleFrequencyBO simpleFrequencyBO;
    @Inject
    private UnityScheduleBO unityScheduleBO;
    @Inject
    private WorkBO workBO;
    @Inject
    private CampaignBO campaignBO;
    @Inject
    private HbBO hbBO;
    @Inject
    private WeightAndHeightBO weightAndHeightBO;
    @Inject
    private DeletesBO deletesBO;
    @Inject
    private ConfigurationsBO configurationsBO;
    private SynchController server;

    public SynchController getServer() {
        return server;
    }

    public void setServer(SynchController server) {
        this.server = server;
    }

    public SynchControllerFacade getSynchControllerDAO() {
        return synchControllerDAO;
    }

    public List<String> loadFilesList() {
        Configurations configurations = configurationsBO.configurationsPerName("defaultfolder");
        List<String> filesList = new ArrayList<String>();
        try {
            File file = new File(configurations.getVal() + GOPATH);
            for (int i = 0; i < file.listFiles().length; i++) {
                if (file.listFiles()[i].getName().contains(".zip")) {
                    filesList.add(file.listFiles()[i].getName());
                }
            }

        } catch (Exception e) {
            e.getMessage();
        }
        return filesList;
    }

    public void createSynchronizationFile(Long serialNumberServer) {
        SynchController synchController = getSynchControllerDAO().findSynchControllerPerSerialNumber(serialNumberServer);
        Calendar currentTime = Calendar.getInstance();
        Configurations configurations = configurationsBO.configurationsPerName("defaultfolder");
        Long serialNumberClient = Long.parseLong(configurationsBO.configurationsPerName("serialnumber").getVal());

        String dirPath = configurations.getVal() + getRETURNSPATH() + "return-"
                + CalendarUtil.getFormatedDate(currentTime).replace(":", "_").replace(" ", "_")
                + "-" + serialNumberServer + "-" + serialNumberClient;
        File directory = new File(dirPath);
        directory.mkdir();

        File file = new File(dirPath + "/" + "return-"
                + CalendarUtil.getFormatedDateTimestamp(currentTime).replace(":", "_").replace(" ", "_")
                + "-" + serialNumberServer + ".tag");

        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(serialNumberClient);
            out.writeObject((synchController.getLastReceived() != null) ? synchController.getLastReceived() : Calendar.getInstance());
            out.writeObject(currentTime);
            
            int size = execute(out, synchController.getLastUpdated(), currentTime, serialNumberServer);
            
            if(size == 0){
                System.out.println("Arquivo vazio");
            }
            
            out.close();
            ZipUtils.compress(directory, new File(dirPath + ".zip"));
            ZipUtils.deleteDir(directory);
        } catch (Exception e) {
            LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Não criou o arquivo de sincronização",e);
        }
    }

    public Long getMasterSerial() {
        Configurations configurations = configurationsBO.configurationsPerName("masterserial");
        return Long.valueOf(configurations.getVal());
    }

    protected int execute(ObjectOutputStream out, Calendar initialDate, Calendar finalDate, Long serialNumber) throws IOException {
        int count = 0;
        for (Neighborhood neighborhood : NeighborhoodBO.modifiedNeighborhoodBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(neighborhood);
            count++;
        }
        
        for (Street street : StreetBO.modifiedStreetBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(street);
            count++;
        }
        
        for (Address address : addressBO.modifiedAddressBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(address);
            count++;
        }
        
        for (Human human : humanBO.modifiedHumanBetweenDate(initialDate, finalDate, serialNumber)) {
            human.getMother();
            human.getFather();
            human.getSponsor();
            human.getAdditionalInfos();
            humanBO.getHumanDAO().detach(human);
            out.writeObject(human);
            count++;
        }
        
        for (Juridic juridic : juridicBO.modifiedJuridicBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(juridic);
            count++;
        }
        
        for (AdditionalInfo additionalInfo : additionalInfoBO.modifiedAddInfoBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(additionalInfo);
            count++;
        }
        
//        for (QuestionsPerson questionsPerson : questionsPersonBO.modifiedQuestionsPersonBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(questionsPerson);
//            count++;
//        }
//        
//        for (QuestResult questResult : questResultBO.modifiedQuestResultBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(questResult);
//            count++;
//        }
//        
        for (Login login : loginBO.modifiedLoginBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(login);
            count++;
        }
       for (Unity unity : unityBO.modifiedUnitiesBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(unity);
            count++;
       }
//        
//        for (Plan plan : planBO.modifiedPlanBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(plan);
//            count++;
//        }
//      
     
       for (Actor actor : actorBO.modifiedActorBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(actor);
              System.out.println("DTINICIAL:"+initialDate.get(Calendar.DAY_OF_MONTH) + "/" + initialDate.get(Calendar.MONTH) + "/" + initialDate.get(Calendar.YEAR) );
       System.out.println("DTFINAL:"+finalDate.get(Calendar.DAY_OF_MONTH) + "/" + finalDate.get(Calendar.MONTH) + "/" + finalDate.get(Calendar.YEAR));
           count++;
           
           System.out.println(actor.getId());
       }
        
        for (UnitySchedule unitySchedule : unityScheduleBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(unitySchedule);
            count++;
        }
        
        for (PerformanceFrequency performanceFrequency : performanceFrequencyBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(performanceFrequency);
            count++;
        }
        
       for (SimpleFrequency simpleFrequency : simpleFrequencyBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(simpleFrequency);
          count++;
       }
//        
//        for (Performance performance : performanceBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(performance);
//            count++;
//        }
//        
//        for (WorkTopic topic : workBO.modifiedTopics(initialDate, finalDate, serialNumber)) {
//            out.writeObject(topic);
//            count++;
//        }
//        
//        for (Work work : workBO.modifiedWorks(initialDate, finalDate, serialNumber)) {
//            out.writeObject(work);
//            count++;
//        }
//        
//        for (Campaign campaign : campaignBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(campaign);
//            count++;
//        }
//        
//        for (Hb hb : hbBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(hb);
//            count++;
//        }
//        
//        for (WeightAndHeight weightAndHeight : weightAndHeightBO.modifiedPerfFreqBetweenDate(initialDate, finalDate, serialNumber)) {
//            out.writeObject(weightAndHeight);
//            count++;
//        }
//        
        for (Deletes deletes : deletesBO.DeletesBetweenDate(initialDate, finalDate, serialNumber)) {
            out.writeObject(deletes);
            count++;
        }
        
        return count;
    }

    public Long readSynchronizationZip(String zipName) {
        Long serial = null;
        Configurations configurations = configurationsBO.configurationsPerName("defaultfolder");
        File zippedFile = new File(configurations.getVal() + GOPATH + zipName);
        try {
            ZipUtils.extract(zippedFile, new File(configurations.getVal() + GOPATH));
        } catch (Exception e) {
            e.printStackTrace();
        }

        File file = new File(configurations.getVal() + GOPATH + zipName.replace(".zip", ""));

        for (int i = 0; i < file.listFiles().length; i++) {
            serial = readSynchronizationFile(file.listFiles()[i].getAbsolutePath());
        }

        return serial;
    }

    public Long readSynchronizationFile(String fileName) {

        File file = new File(fileName);
        
        Long serial = null;
        Calendar lastUpdated = null;
        
        FileInputStream input = null;
        ObjectInputStream in = null;
        Object abstractEntity;

        try {
            input = new FileInputStream(file);
            in = new ObjectInputStream(new FileInputStream(file));
            
            serial = (Long) in.readObject();
            lastUpdated = (Calendar) in.readObject();
            Calendar syncTime = (Calendar) in.readObject();
            
            server = synchControllerDAO.findSynchControllerPerSerialNumber(serial);
            if(server.getLastReceived() != null
                    //já axiste uma atualização mais recente na máquina
                    && server.getLastReceived().after(lastUpdated)) {            
                return serial;
            }
            
            try {
                abstractEntity = in.readObject();
            } catch(EOFException e) {
                abstractEntity = null;
                serial = null;
                LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Arquivo vazio",e);
                throw new Exception("Arquivo vazio");
            }
            
            while (abstractEntity != null){
                System.out.println(abstractEntity.getClass().getSimpleName());
                if (AbstractEntity.class.isInstance(abstractEntity)) {
                    ((AbstractEntity) abstractEntity).setSyncing(true);
                }

                if (abstractEntity.getClass().equals(Street.class)) {
                    Street street = (Street) abstractEntity;
                    street.setSerialNumber(serial);
                    if (StreetBO.find(street.getId()) == null) {
                        StreetBO.create(street);
                    } else {
                        StreetBO.edit(street);
                    }
                } else if (abstractEntity.getClass().equals(Neighborhood.class)) {
                    Neighborhood neighborhood = (Neighborhood) abstractEntity;
                    neighborhood.setSerialNumber(serial);
                    if (NeighborhoodBO.find(neighborhood.getId()) == null) {
                        NeighborhoodBO.create(neighborhood);
                    } else {
                        NeighborhoodBO.edit(neighborhood);
                    }
                } else if (abstractEntity.getClass().equals(Address.class)) {
                    Address address = (Address) abstractEntity;
                    address.setSerialNumber(serial);
                    if (addressBO.find(address.getId()) == null) {
                        addressBO.create(address);
                    } else {
                        addressBO.edit(address);
                    }
                } else if (abstractEntity.getClass().equals(Human.class)) {
                    Human human = (Human) abstractEntity;
                    human.setSerialNumber(serial);
                    insertHumans(human);
                    humanBO.getHumanDAO().flush();
                } else if (abstractEntity.getClass().equals(Juridic.class)) {
                    Juridic juridic = (Juridic) abstractEntity;
                    juridic.setSerialNumber(serial);
                    if (juridicBO.find(juridic.getId()) == null) {
                        juridicBO.create(juridic);
                    } else {
                        juridicBO.edit(juridic);
                    }
                } else if (abstractEntity.getClass().equals(AdditionalInfo.class)) {
                    AdditionalInfo additionalInfo = (AdditionalInfo) abstractEntity;
                    additionalInfo.setSerialNumber(serial);
                    if (additionalInfoBO.find(additionalInfo.getId()) == null) {
                        additionalInfoBO.create(additionalInfo);
                    } else {
                        additionalInfoBO.edit(additionalInfo);
                    }
                } else if (abstractEntity.getClass().equals(Login.class)) {
                    Login login = (Login) abstractEntity;
                    login.setSerialNumber(serial);
                    if (loginBO.find(login.getId()) == null) {
                        loginBO.create(login);
                    } else {
                        loginBO.edit(login);
                    }
              } else if (abstractEntity.getClass().equals(Unity.class)) {
                   Unity unity = (Unity) abstractEntity;
                   unity.setSerialNumber(serial);
                   insertUnitys(unity);
//            } else if (abstractEntity.getClass().equals(Plan.class)) {
//                    Plan plan = (Plan) abstractEntity;
//                    plan.setSerialNumber(serial);
//                    if (planBO.find(plan.getId()) == null) {
//                        planBO.create(plan);
//                    } else {
//                        planBO.edit(plan);
//                    }
               } else if (abstractEntity.getClass().equals(Actor.class)) {
                   Actor actor = (Actor) abstractEntity;
                   actor.setSerialNumber(serial);
                   System.out.println("ActorP: "+actor.getPersonage().getId()+" "+actor.getPersonage().getName());
                    if (actorBO.find(actor.getId()) == null) {
                        actorBO.create(actor);
                   } else {
                        actorBO.edit(actor);
                   }
                    actorBO.getActorDAO().flush();
                } else if (abstractEntity.getClass().equals(UnitySchedule.class)) {
                    UnitySchedule us = (UnitySchedule) abstractEntity;
                    us.setSerialNumber(serial);
                    if (unityScheduleBO.find(us.getId()) == null) {
                        unityScheduleBO.create(us);
                    } else {
                        unityScheduleBO.edit(us);
                    }
               } else if (abstractEntity.getClass().equals(PerformanceFrequency.class)) {
                   PerformanceFrequency performanceFrequency = (PerformanceFrequency) abstractEntity;
                   performanceFrequency.setSerialNumber(serial);
                    if (performanceFrequencyBO.find(performanceFrequency.getId()) == null) {
                       performanceFrequencyBO.create(performanceFrequency);
                   } else {
                       performanceFrequencyBO.edit(performanceFrequency);
                   }
              } else if (abstractEntity.getClass().equals(SimpleFrequency.class)) {
                   SimpleFrequency simpleFrequency = (SimpleFrequency) abstractEntity;
                    simpleFrequency.setSerialNumber(serial);
                    if (simpleFrequencyBO.find(simpleFrequency.getId()) == null) {
                       simpleFrequencyBO.create(simpleFrequency);
                   } else {
                       simpleFrequencyBO.edit(simpleFrequency);
                    }
//                } else if (abstractEntity.getClass().equals(Performance.class)) {
//                    Performance performance = (Performance) abstractEntity;
//                    performance.setSerialNumber(serial);
//                    if (performanceBO.find(performance.getId()) == null) {
//                        performanceBO.create(performance);
//                    } else {
//                        performanceBO.edit(performance);
//                    }
//                }  else if (abstractEntity.getClass().equals(WorkTopic.class)) {
//                    WorkTopic topic = (WorkTopic) abstractEntity;
//                    topic.setSerialNumber(serial);
//                    if (workBO.getTopicFacade().find(topic.getId()) == null) {
//                        workBO.getTopicFacade().create(topic);
//                    } else {
//                         workBO.getTopicFacade().edit(topic);
//                    }
//                }  else if (abstractEntity.getClass().equals(Work.class)) {
//                    Work work = (Work) abstractEntity;
//                    work.setSerialNumber(serial);
//                    if (workBO.getWorkFacade().find(work.getId()) == null) {
//                        workBO.getWorkFacade().create(work);
//                    } else {
//                        workBO.getWorkFacade().edit(work);
//                    }
                    
//###########################                  
//#####       HB        #####
//###########################
                    
//                } else if (abstractEntity.getClass().equals(Campaign.class)) {
//                    Campaign campaign = (Campaign) abstractEntity;
//                    campaign.setSerialNumber(serial);
//                    if (campaignBO.find(campaign.getId()) == null) {
//                        campaignBO.create(campaign);
//                    } else {
//                        campaignBO.edit(campaign);
//                    }
//                } else if (abstractEntity.getClass().equals(WeightAndHeight.class)) {
//                    WeightAndHeight weightAndHeight = (WeightAndHeight) abstractEntity;
//                    weightAndHeight.setSerialNumber(serial);
//                    if (weightAndHeightBO.find(weightAndHeight.getId()) == null) {
//                        weightAndHeightBO.create(weightAndHeight);
//                    } else {
//                        weightAndHeightBO.edit(weightAndHeight);
//                    }
//                } else if (abstractEntity.getClass().equals(Hb.class)) {
//                    Hb hb = (Hb) abstractEntity;
//                    hb.setSerialNumber(serial);
//                    if (hbBO.find(hb.getId()) == null) {
//                        hbBO.create(hb);
//                    } else {
//                        hbBO.edit(hb);
//                    }
                } else if (abstractEntity.getClass().equals(Deletes.class)) {
                    executeDelete((Deletes) abstractEntity);
                }
                try{
                    abstractEntity = in.readObject();
                }catch(EOFException e){
                    abstractEntity = null;
                }
            }
            server.setLastUpdated(lastUpdated);
            server.setLastReceived(syncTime);
            this.edit(server);
        } catch (IOException e) {
            serial = null;
            LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Não leu arquivo de sincronização",e);
        } catch (ClassNotFoundException e) {
            serial = null;
            LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Não identificou classe na sincronização",e);
        } catch (Exception e) {
            serial = null;
            LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Não leu arquivo de sincronização",e);
        } finally {
            try{
                in.close();
                input.close();
            }catch (Exception e) {
                LoggerUtil.log(SyncBO.class.getName(),Level.SEVERE,"Não leu arquivo de sincronização",e);
            }
        }

        return serial;
    }

    public void insertHumans(Human human){
        if(human.getMother() != null){
            insertHumans(human.getMother());
        }
        if(human.getFather() != null){
            insertHumans(human.getFather());
        }
        if(human.getSponsor() != null){
            insertHumans(human.getSponsor());
        }
        if (humanBO.find(human.getId()) == null) {
            human.setAdditionalInfos(null);
            humanBO.getHumanDAO().syncCreate(human);
        } else {
            humanBO.edit(human);
        }
    }

    public void insertUnitys(Unity unity) {
        System.out.println("Unity: "+unity.getName());
        if(unity.getUnityFather() != null){
            insertUnitys(unity.getUnityFather());
        }
        
        if (unityBO.find(unity.getId()) == null) {
             unityBO.create(unity);
        } else {
            unityBO.edit(unity);
        }
    }

    public void executeDelete(Deletes delete) throws IOException {
        if (delete.getTargetTable().equals("street")) {
            Street street = StreetBO.find(delete.getHashID());
            if(street!=null){
                StreetBO.remove(street);
            }
        } else if (delete.getTargetTable().equals("neighborhood")) {
            Neighborhood neighborhood = NeighborhoodBO.find(delete.getHashID());
            if(neighborhood!=null){
                NeighborhoodBO.remove(neighborhood);
            }
        } else if (delete.getTargetTable().equals("address")) {
            Address address = addressBO.find(delete.getHashID());
            if(address!=null){
                addressBO.remove(address);
            }
        } else if (delete.getTargetTable().equals("human")) {
            Human human = humanBO.find(delete.getHashID());
            if(human!=null){
                humanBO.remove(human);
            }
        } else if (delete.getTargetTable().equals("juridic")) {
            Juridic juridic = juridicBO.find(delete.getHashID());
            if(juridic!=null){
                juridicBO.remove(juridic);
            }
        } else if (delete.getTargetTable().equals("additionalinfo")) {
            AdditionalInfo additionalInfo = additionalInfoBO.find(delete.getHashID());
            if(additionalInfo!=null){
                additionalInfoBO.remove(additionalInfo);
            }
        } else if (delete.getTargetTable().equals("questionsperson")) {
            QuestionsPerson questionsPerson = questionsPersonBO.find(delete.getHashID());
            if(questionsPerson!=null){
                questionsPersonBO.remove(questionsPerson);
            }
        } else if (delete.getTargetTable().equals("questresult")) {
            QuestResult questResult = questResultBO.find(delete.getHashID());
            if(questResult!=null){
                questResultBO.remove(questResult);
            }
        } else if (delete.getTargetTable().equals("login")) {
            Login login = loginBO.find(delete.getHashID());
            if(login!=null){
                loginBO.remove(login);
            }
        } else if (delete.getTargetTable().equals("unity")) {
            Unity unity = unityBO.find(delete.getHashID());
            if(unity!=null){
                unityBO.remove(unity);
            }
        } else if (delete.getTargetTable().equals("plan")) {
            Plan plan = planBO.find(delete.getHashID());
            if(plan!=null){
                planBO.remove(plan);
            }
        } else if (delete.getTargetTable().equals("actor")) {
            Actor actor = actorBO.find(delete.getHashID());
            if(actor!=null){
                actorBO.remove(actor);
            }
        } else if (delete.getTargetTable().equals("unityschedule")) {
            UnitySchedule unitySchedule = unityScheduleBO.find(delete.getHashID());
            if(unitySchedule!=null){
                unityScheduleBO.remove(unitySchedule);
            }
        } else if (delete.getTargetTable().equals("simplefrequency")) {
            SimpleFrequency simpleFrequency = simpleFrequencyBO.find(delete.getHashID());
            if(simpleFrequency!=null){
                simpleFrequencyBO.remove(simpleFrequency);
            }
        } else if (delete.getTargetTable().equals("performancefrequency")) {
            PerformanceFrequency performanceFrequency = performanceFrequencyBO.find(delete.getHashID());
            if(performanceFrequency!=null){
                performanceFrequencyBO.remove(performanceFrequency);
            }
        } else if (delete.getTargetTable().equals("performance")) {
            Performance performance = performanceBO.find(delete.getHashID());
            if(performance!=null){
                performanceBO.remove(performance);
            }
        } else if (delete.getTargetTable().equals("campaign")) {
            Campaign campaign = campaignBO.find(delete.getHashID());
            if(campaign!=null){
                campaignBO.remove(campaign);
            }
        } else if (delete.getTargetTable().equals("hb")) {
            Hb hb = hbBO.find(delete.getHashID());
            if(hb!=null){
                hbBO.remove(hb);
            }
        } else if (delete.getTargetTable().equals("weightandheight")) {
            WeightAndHeight weightAndHeight = weightAndHeightBO.find(delete.getHashID());
            if(weightAndHeight!=null){
                weightAndHeightBO.remove(weightAndHeight);
            }
        }
    }

    public void create(SynchController synchController) {
        getSynchControllerDAO().create(synchController);
    }

    public void edit(SynchController synchController) {
        getSynchControllerDAO().edit(synchController);
    }

    public void remove(SynchController synchController) {
        getSynchControllerDAO().remove(synchController);
    }

    public SynchController find(Object id) {
        return getSynchControllerDAO().find(id);
    }

    public List<SynchController> findAll() {
        return getSynchControllerDAO().findAll();
    }

    public String getRETURNSPATH() {
        return RETURNSPATH;
    }

    public String getGOPATH() {
        return GOPATH;
    }
}
